/*
 * This file is part of animus, a GUI for the command line app "GHost++"
 *
 * http://code.google.com/p/animus/
 *
 * Copyright (C) 2008-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */


/*
 * test
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace animus
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            #region Check for orphaned ghosts (poor things!)
            if (Properties.Settings.Default.CheckForOrphanedGHosts && !FormMain.IsRunningOnMono())
            {
                Process[] ghosts = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Properties.Settings.Default.GhostExecutable));
                List<Process> orphans = new List<Process>();
                foreach (Process p in ghosts)
                {
                    try
                    {
                        ProcessHelper.GetParentProcess(p.Id);
                    }
                    catch (ApplicationException)
                    {
                        orphans.Add(p);
                    }
                }
                if (orphans.Count > 0 &&
                    MessageBox.Show(String.Format("There are {0} ghost processes running with no GUI Controller attached. Kill them?", orphans.Count), "Orphaned process found", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (Process p in orphans)
                    {
                        try
                        {
                            if (!p.CloseMainWindow())
                                p.Kill();

                        }
                        catch
                        {
                            //only catch
                        }
                    }
                }
            }
            #endregion

            FormMain mainForm = new FormMain();
            if (!Properties.Settings.Default.StartMinimized)
                mainForm.Show();
            Application.Run();
        }
    }
}